/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.bootstrap.client.ui;

import java.util.List;

import com.cleangwt.bootstrap.client.style.Btn;
import com.cleangwt.uibinder.client.TemplateSupport;
import com.cleangwt.user.client.AnchorActionEvent;
import com.cleangwt.user.client.AnchorActionEvent.AnchorActionHandler;
import com.cleangwt.user.client.AnchorActionEvent.HasAnchorActionHandlers;
import com.cleangwt.user.client.HashTokenizer;
import com.cleangwt.user.client.ui.RequiresOptions;
import com.cleangwt.user.shared.Option;
import com.google.gwt.dom.client.AnchorElement;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Widget;

/**
 * The button group to put button widgets together.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class ButtonGroup extends Widget implements HasAnchorActionHandlers, RequiresOptions {

  private static final String HORIZONTAL_STYLE = "btn-group";
  private static final String VERTICAL_STYLE = "btn-group-vertical";

  /**
   * Creates a button group that wraps an existing &lt;div&gt; element of template.
   * 
   * @param template the template
   * @param element the element to be wrapped
   */
  public static ButtonGroup wrap(TemplateSupport template, Element element) {
    ButtonGroup widget = new ButtonGroup(element);
    template.wrap(widget);
    return widget;
  }

  private boolean selectionHandlerInitialized;

  /**
   * Creates a button group.
   */
  public ButtonGroup() {
    this(false);
  }

  /**
   * Creates a button group
   * 
   * @param veritical <code>true</code> to lay buttons vertically
   */
  public ButtonGroup(boolean vertical) {
    this.setElement(Document.get().createDivElement());
    if (vertical) {
      setStyleName(VERTICAL_STYLE);
    } else {
      setStyleName(HORIZONTAL_STYLE);
    }
  }

  /**
   * This constructor may be used by subclasses to explicitly use an existing element. This element
   * must be a &lt;div&gt; element.
   * 
   * @param element the element to be used
   */
  protected ButtonGroup(Element element) {
    assert element.getTagName().equalsIgnoreCase(DivElement.TAG);
    this.setElement(element);
  }

  /**
   * Creates a button group with initial options.
   * 
   * @param options the options
   */
  public ButtonGroup(List<Option> options) {
    setOptions(options);
  }

  @Override
  public HandlerRegistration addAnchorActionHandler(AnchorActionHandler handler) {
    if (!this.selectionHandlerInitialized) {
      selectionHandlerInitialized = true;
      this.addDomHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
          AnchorActionEvent.translate(ButtonGroup.this, event);
        }
      }, ClickEvent.getType());
    }
    return addHandler(handler, AnchorActionEvent.getType());
  }

  /**
   * Adds a button with the given inner html and name.
   * 
   * @param html the inner html for button
   * @param name the name for {@link AnchorActionEvent}
   */
  public Element addButton(String html, String action) {
    AnchorElement element = Document.get().createAnchorElement();
    element.setInnerHTML(html);
    element.setHref(HashTokenizer.packActionHash(action));
    element.setClassName(Btn.BTN_STYLE);
    element.addClassName(Btn.DEFAULT_STYLE);
    getElement().appendChild(element);
    return element;
  }

  /**
   * Adds a button dropdown with the given toggle text and returns the {@link DropdownBuilder} to
   * build it.
   * 
   * @param toggleText the text for toggle element
   * @return the dropdown builder to build a complete button dropdown
   */
  public DropdownBuilder addDropdown(String toggleText) {
    Element dropdown = DropdownBuilder.createButtonDropdown(toggleText);
    this.getElement().appendChild(dropdown);
    return new DropdownBuilder(dropdown);
  }

  /**
   * Sets the options to initialize buttons.
   * 
   * @param options the options to be rendered to a navigation widgets.
   */
  @Override
  public void setOptions(List<Option> options) {
    this.getElement().setInnerText("");
    if (options != null) {
      for (final Option option : options) {
        addButton(option.getName(), option.getValue());
      }
    }
  }
}
